<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>用户列表</title>
		<link rel="stylesheet" href="js/bower_components/bootstrap/dist/css/bootstrap.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-select/dist/css/bootstrap-select.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-switch/dist/css/bootstrap3/bootstrap-switch.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-table/dist/bootstrap-table.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.css">
		<link href="js/bower_components/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css"
			rel="stylesheet">

		<script src="js/bower_components/jquery/dist/jquery.min.js"></script>
		<script src="js/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
		<script src="js/bower_components/bootbox.min.js"></script>
		<script src="js/bower_components/bootstrap-select/js/bootstrap-select.js"></script>
		<script src="js/bower_components/bootstrap-switch/dist/js/bootstrap-switch.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/bootstrap-table.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>

		<script src="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.js"></script>
		<script src="js/bower_components/jquery-form-validator/form-validator/jquery.form-validator.min.js"></script>
		<script src="js/bower_components/moment/min/moment-with-locales.min.js"></script>
		<script src="js/bower_components/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>

		<script>
			$(function() {
				//数据初始化 
				getDate()

				//新增功能
				$("#btnAdd").click(function() {
					$("#myModelLable").text("新增日志信息")
					$("#divMyUserFrom").modal();
				})

				//保存数据
				$("#butSave").click(function() {
					var id = $("#userid").val();
					console.log(id);
					if (id >= 1) {
						// 修改信息
						MyUser = {
							"ipaddress": $("#ipaddress").val(),
							"opetime": $("#opetime").val(),
							"opename": $("#opename").val(),
							"userid": id
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/updateLog",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("修改数据后返回的结果为: ")
								console.log(res);
								if (res > 0) {
									alert("保存成功");
									$("#divMyUserFrom").modal('hide')
									getDate();
								}
							}
						});
					} else {
						MyUser = {
							"ipaddress": $("#ipaddress").val(),
							"opetime": $("#opetime").val(),
							"opename": $("#opename").val()
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/addLog",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("新增数据后返回的结果为: ")
								console.log(res);
								if (res > 0) {
									alert("保存成功");
									$("#divMyUserFrom").modal('hide')
									getDate();
								}
							}
						});
					}
				})

				//给修改按钮添加事件
				$("#btnUpdate").click(function() {
					//获取修改的主键
					var userid = getSelectID();
					console.log(userid);
					$.ajax({
						type: "get",
						url: "http://localhost:8082/mylog/getLog?userid=" + userid,
						contentType: "application/json;charset=utf-8",
						dataType: "json",
						success: function(res) {
							console.log("根据主键获取后端返回的结果为: ")
							console.log(res);
							if (res != null) {
								$("#myModelLable").text("修改用户信息");
								$("#username").val(res["username"]);
								$("#pwd").val(res["pwd"]);
								$("#userid").val(res["userid"]);
								$("#divMyUserFrom").modal();
							}
						}
					});
				})

				//查找主键
				function getSelectID() {
					var id = $("#tbMyUser").bootstrapTable('getSelections');
					console.log(id);
					if (id.length > 0) {
						return id[0].userid;
					} else {
						bootbox.alert("必须选择一条要修改的信息!")
					}
				}
				
				//删除列表当中的信息
				$("#btnDel").click(function(){
					var userid=getSelectID();
					console.log(userid);
					$.ajax({
						type: "get",
						url: "http://localhost:8082/mylog/deleteLog?userid=" + userid,
						contentType: "application/json;charset=utf-8",
						dataType: "json",
						success: function(res) {
							console.log("根据主键删除数据并返回的结果为: ")
							console.log(res);
							if (res==1) {
								bootbox.alert("删除成功");
								getDate();
							}
						}
					});
				})

				//给搜索按钮添加点击事件
				$("#btnSelect1").click(function() {
					var ipaddress = $("#txtUsername1").val();
					console.log(ipaddress);
					if (ipaddress.length == 0) {
						getDate()
					} else {
						MyUser = {
							"beginPage": 0,
							"pageCount": 5,
							"ipaddress": ipaddress
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/getAll",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("获取分页后的数据, 返回结果是: ")
								console.log(res);
								setTable(res)
							}
						});
					}
				})
				
				$("#btnSelect2").click(function() {
					var ipaddress = $("#txtUsername1").val();
					console.log("ip地址为:"+ipaddress);
					var opetime = $("#txtUsername2").val();
					console.log("操作时间为:"+opetime);
					if (opetime.length == 0 && ipaddress.length==0) {
						getDate()
					} else if (ipaddress.length == 0){
						MyUser = {
							"beginPage": 0,
							"pageCount": 5,
							"opetime": opetime
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/getAll",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("获取分页后的数据, 返回结果是: ")
								console.log(res);
								setTable(res)
							}
						});
					} else if (opetime.length == 0){
						MyUser = {
							"beginPage": 0,
							"pageCount": 5,
							"ipaddress": ipaddress
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/getAll",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("获取分页后的数据, 返回结果是: ")
								console.log(res);
								setTable(res)
							}
						});
					}else{
						MyUser = {
							"beginPage": 0,
							"pageCount": 5,
							"ipaddress": ipaddress,
							"opetime": opetime
						};
						$.ajax({
							type: "put",
							url: "http://localhost:8082/mylog/getAll",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyUser),
							dataType: "json",
							success: function(res) {
								console.log("获取分页后的数据, 返回结果是: ")
								console.log(res);
								setTable(res)
							}
						});
					}
				})
			})

			//获取列表数据
			function getDate() {
				MyUser = {
					"beginPage": 0,
					"pageCount": 5
				};
				$.ajax({
					type: "put",
					url: "http://localhost:8082/mylog/getAll",
					contentType: "application/json;charset=utf-8",
					data: JSON.stringify(MyUser),
					dataType: "json",
					success: function(res) {
						console.log("获取分页后的数据, 返回结果是: ")
						console.log(res);
						setTable(res)
					}
				});
			}

			//将数据绑定在表格上
			function setTable(myUserList) {
				$("#tbMyUser").bootstrapTable('destroy')
				$("#tbMyUser").bootstrapTable({
					data: myUserList,
					striped: true,
					contentType: "application/x-www-form-urlencoded;charset=utf-8",
					uniqueId: "value",
					idField: 'userid',
					checkboxHeader: true,
					singleSelect: true,
					clickToSelect: true,
					columns: [{
						field: "userid",
						align: "center",
						visible: false
					}, {
						field: "ipaddress",
						title: "IP地址",
						align: "center"
					}, {
						field: "opetime",
						title: "操作时间",
						align: "center"
					}, {
						field: "opename",
						title: "操作名称",
						// checkbox: true,
						align: "center"
					},{
						title: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;选中进行修改 / 删除&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
						checkbox: true,
						align: "center"
					}]
				})
			}
		</script>

	</head>

	<body>
		<!--查询区域-->
		<div class="input-group">
			IP地址: <input type="text" id="txtUsername1" /> &nbsp;&nbsp;
			<br />
			日&nbsp;&nbsp;&nbsp;期: <input type="text" id="txtUsername2" /> &nbsp;&nbsp;
			<button type="button" id="btnSelect2" class="btn btn-default">搜索</button>
		</div>
		<!--功能区域-->
		<div style="margin: 5px;margin-top:10px;">
			<button type="button" id="btnAdd" class="btn btn-success">新增</button>
			<button type="button" id="btnUpdate" class="btn btn-info">修改</button>
			<button type="button" id="btnDel" class="btn btn-danger">删除</button>
		</div>
		<br />
		<!--列表区域-->
		<div class="row col-md-10" style="margin-top: 10px;">
			<table id="tbMyUser"></table>
		</div>

		<!--模态框当中涵盖的表单功能-->
		<div class="modal fade" id="divMyUserFrom" tabindex="-1" role="dialog" aria-labelledby="MyModel"
			aria-hidden="true">
			<div class="modal-dialog" style="width: 400px;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-disimiss="modal" aria-hidder="true">&times;</button>
						<h4 class="modal-title" id="myModelLable">用户信息</h4>
						<input type="hidden" id="userid" />
					</div>
					<div class="modal-body form-inline">
						<div class="form-group">
							<label class="control-label" for="ipaddress">I&nbsp;P&nbsp;&nbsp;地&nbsp;址:</label>
							<input style="width: 220px;" class="form-control" type="text" id="ipaddress" />
						</div>
						<br />
						<br />
						<div class="form-group">
							<label class="control-label" for="opetime">操作时间:</label>
							<input style="width: 220px;" class="form-control" type="text" id="opetime" />
						</div>
						<br />
						<br />
						<div class="form-group">
							<label class="control-label" for="opename">操作名称:</label>
							<input style="width: 220px;" class="form-control" type="text" id="opename" />
						</div>
						<br />
						<br />
						<div class="modal-footer">
							<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
							<button type="button" class="btn btn-primary" id="butSave">保存</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
